package com.th.supcom.pds.auditprescription.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.AggregationRenderer;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.utils.ClientUtils;

/**
 * add by yzhang
 */
public class AuditPrescripInfoGridPanel extends ContentPanel
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    /** 库存定义表. */
    private Grid <PdsSupplyDetailModel> m_grid;

    /** 库存定义储器. */
    private ListStore <PdsSupplyDetailModel> m_store;

    public AuditPrescripInfoGridPanel ()
    {
        init ();
    }

    private AggregationRowConfig <PdsSupplyDetailModel> createAggregationRowConfig ()
    {
        final AggregationRowConfig <PdsSupplyDetailModel> config = new AggregationRowConfig <PdsSupplyDetailModel> ();
        final NumberFormat number = NumberFormat.getFormat ("0.00");
        config.setHtml ("qtyUnitName", "合计");
        config.setSummaryType ("costs", SummaryType.SUM);
        config.setRenderer ("costs", new AggregationRenderer <PdsSupplyDetailModel> ()
        {

            @Override
            public Object render (final Number value, final int colIndex, final Grid <PdsSupplyDetailModel> grid, final ListStore <PdsSupplyDetailModel> store)
            {
                String displayValue = "0.00";
                if (null != value)
                {
                    displayValue = number.format (value).toString ();
                }
                return "<span style='color:red;font-weight:bold;float:right;'>" + displayValue + "</span>";
            }
        });
        return config;
    }

    /**
     * 初始化表格
     * 
     * @return
     */
    private ColumnModel createGridHeader ()
    {
        final List <ColumnConfig> columns = new ArrayList <ColumnConfig> ();
        final RowNumberer r = new RowNumberer ();
        columns.add (r);

        ColumnConfig column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "phamName", "药品名称", HorizontalAlignment.LEFT, 150, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "phamSpec", "药品规格", HorizontalAlignment.LEFT, 110, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "unitQty", "包装数量", HorizontalAlignment.LEFT, 50, getRenderer ("unitQty"));
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "packageUnitName", "包装单位", HorizontalAlignment.LEFT, 50, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "packagePrice", "包装价格", HorizontalAlignment.LEFT, 50, getRenderer ("packagePrice"));
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "costs", "金额", HorizontalAlignment.LEFT, 45, getRenderer ("costs"));
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "freqTimeQty", "服法", HorizontalAlignment.LEFT, 100, ClientUtils.createUsageRenderer ());
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "memo", "备注", HorizontalAlignment.LEFT, 60, null);
        columns.add (column);

        final ColumnModel columnModel = new ColumnModel (columns);
        columnModel.addAggregationRow (createAggregationRowConfig ());
        return columnModel;
    }

    public Grid <PdsSupplyDetailModel> getGrid ()
    {
        return m_grid;
    }

    private GridCellRenderer <PdsSupplyDetailModel> getRenderer (final String str)
    {
        final GridCellRenderer <PdsSupplyDetailModel> change = new GridCellRenderer <PdsSupplyDetailModel> ()
        {
            final NumberFormat m_number = NumberFormat.getFormat ("0.00");

            @Override
            public Object render (final PdsSupplyDetailModel model, final String property, final ColumnData config, final int rowIndex, final int colIndex,
                                  final ListStore <PdsSupplyDetailModel> store, final Grid <PdsSupplyDetailModel> grid)
            {
                if (model.get (str) == null)
                {
                    return "<span></span>";
                }
                final Object value = "price".equals (str) ? m_number.format ((Double) model.get (str)) : model.get (str);
                return "<span style='float:right'>" + value.toString () + "</span>";
            }
        };
        return change;
    }

    public ListStore <PdsSupplyDetailModel> getStore ()
    {
        return m_store;
    }

    private void init ()
    {
        // 初始化中心面板
        setHeaderVisible (false);
        setLayout (new FitLayout ());
        m_grid = this.initPrescripInfo ();
        add (m_grid);
    }

    private Grid <PdsSupplyDetailModel> initPrescripInfo ()
    {
        final Grid <PdsSupplyDetailModel> grid = new Grid <PdsSupplyDetailModel> (initStore (), createGridHeader ());
        grid.setLoadMask (true);
        grid.getView ().setEmptyText ("暂时没有您需要的数据...");
        grid.setColumnReordering (true);
        grid.setStyleAttribute ("border", "0px");
        grid.setColumnLines (true);
        grid.getView ().setForceFit (true);
        return grid;
    }

    private ListStore <PdsSupplyDetailModel> initStore ()
    {
        m_store = new ListStore <PdsSupplyDetailModel> ();
        return m_store;
    }

    public void setGrid (final Grid <PdsSupplyDetailModel> grid)
    {
        m_grid = grid;
    }

    public void setStoreData (final List <PdsSupplyDetailModel> prescripDetailInfo)
    {
        m_store.add (prescripDetailInfo);
    }
}
